<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->

<cfcomponent output="false">

	<cfset this.id = "" />
	<cfset this.name = "" />
	<cfset this.version = "" />
	<cfset this.provider = "" />
	<cfset this.class = "" />
	<cfset variables.service = "" />
	
	<cffunction name="init" access="public" output="false" returntype="AbsPlugin">
		<cfargument name="pluginConfig" type="xml" required="true" />
        <cfargument name="eventManager" type="fileManager.event.SystemEventManager" required="true"  />
        <cfargument name="sessionMgr" type="fileManager.util.SessionManager" />
		<cfscript>
		this.id = arguments.pluginConfig.plugin.XmlAttributes.id;
		this.name = arguments.pluginConfig.plugin.XmlAttributes.name;
		this.version = arguments.pluginConfig.plugin.XmlAttributes.version;
		this.provider = arguments.pluginConfig.plugin.XmlAttributes.provider;
		this.class = arguments.pluginConfig.plugin.XmlAttributes.class;
		this.type = arguments.pluginConfig.plugin.XmlAttributes.type;
		
		variables.pluginConfig = arguments.pluginConfig;
		
		variables.eventManager = arguments.eventManager ;
		variables.sessionMgr = arguments.sessionMgr;
		
		serviceFactory();
		</cfscript>
		<cfreturn this />
	</cffunction>

	<cffunction name="getService" access="public" returntype="Component">
		<cfreturn variables.service />
	</cffunction>
	
	<!--- private --->
	
	<cffunction name="serviceFactory" access="private" returntype="Any" >
		<cfset var myXml = expandPath( '/fileManager/plugin/#this.name#/config/ColdSpring.xml' ) />
		<cfset var services = arrayNew(1) />
		<cfset var sf = ""  />
		
		<cfif this.type eq "coldspring">
		
			<cfif fileExists( myXml ) >
			
				<cfset sf = createColdSpring( xmlParse( myXml ) ) />
				
				<cfset variables.service = sf.getBean('#this.name#Service') />
				
				<cfset variables.service.setSessionMgr( variables.sessionMgr ) />
				<cfset variables.service.setEventManager( variables.eventManager ) />
				
			</cfif>
			
		<cfelseif this.type eq "class">
		
			<cfset services = xmlSearch( variables.pluginConfig, "/plugin/services/remote/" ) />
		
			<cfloop index="svc" from="1" to="#arrayLen( services )#">
			
				<!--- service without coldspring --->
				<cfset variables.service = createObject( 'component', '#services[ svc ].XmlAttributes['map-to-class']#' ).init() />
				
				<cfset variables.service.setSessionMgr( variables.sessionMgr ) />
				<cfset variables.service.setEventManager( variables.eventManager ) />
				
			</cfloop>
		
		</cfif>
		
	</cffunction>

	<cffunction name="createColdSpring" access="private" returntype="Component" output="false" >
		<cfargument name="xmlObj" type="xml" required="true" />
		
		<cfset var serviceFactory = CreateObject( 'component', 'coldspring.beans.DefaultXmlBeanFactory' ).init( structNew(), structNew() ) />
		
		<cfset serviceFactory.loadBeansFromXmlObj( arguments.xmlObj ) />
		
		<cfreturn serviceFactory />
	</cffunction>

</cfcomponent>